package com.echo.code.arithmetic.chapter08;

import static com.echo.code.arithmetic.chapter08.Collections.*;

import com.echo.code.arithmetic.chapter07.Optional;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by baiyap on 2018/8/15.
 */
public class Greed {

  public static void main(String[] args) {
    Set<String> states_needed = new HashSet<>(Arrays.asList("mt", "wa", "or", "id", "nv", "ut", "ca", "az"));
    Map<String, HashSet<String>> stations = new HashMap<String, HashSet<String>>() {{
      put("kone", new HashSet<>(Arrays.asList("id", "nv", "ut")));
      put("ktwo", new HashSet<>(Arrays.asList("wa", "id", "mt")));
      put("kthree", new HashSet<>(Arrays.asList("or", "nv", "ca")));
      put("kfour", new HashSet<>(Arrays.asList("nv", "ut")));
      put("kfive", new HashSet<>(Arrays.asList("ca", "az")));
    }};
    Set<String> final_stations = new LinkedHashSet<>();


    final Set<String> states_covered = new HashSet<>();
    final Optional<String> best_station = Optional.getInstance();
    while (!states_needed.isEmpty()) {
      states_covered.clear();
      best_station.reset();

      stations.forEach((key, value) -> {
        Set<String> covered = fork(unmodifiableSet(value));
        covered.retainAll(states_needed);
        if (covered.size() > states_covered.size()) {
          best_station.setValue(key);
          states_covered.clear();
          states_covered.addAll(covered);
        }
      });

      states_needed.removeAll(states_covered);
      final_stations.add(best_station.getValue());
    }
    System.out.println("the result is : " + final_stations);
  }
}
